<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Images_model extends CI_Model {

    private $_table_name = 'images';

    //private $upload_path = "FCPATH/upload";

    public function __construct() {
        parent::__construct();
    }

    function create($image) {
        $this->db->set('image_date', 'NOW()', FALSE);
        $insert = $this->db->insert($this->_table_name, $image);
        return $insert;
    }

    public function upload_image() {
        $this->load->library('upload');
        $this->load->library('image_lib');

        if (!$this->upload->do_upload()) {
            $error = array('error' => $this->upload->display_errors());
            echo $error['error'];
            exit();
        } else {
            $data = $this->upload->data();

            //图像封面
            $config['source_image'] = $data['full_path'];
            $config['create_thumb'] = TRUE;
            $config['image_library'] = 'gd2';
            $config['create_thumb'] = TRUE;
            $config['maintain_ratio'] = TRUE;
            $config['master_dim'] = 'width';
            $config['width'] = 160;
            $config['height'] = 160;
            $config['quality'] = 90;

//			$rotation_angle = $this->input->post('rotation_angle');
//			if(in_array($rotation_angle, array('90','180','270','hor','vrt')))
//				$config['rotation_angle'] = $rotation_angle;
            $this->image_lib->initialize($config);
            $this->image_lib->resize();
            $this->image_lib->clear();

            //图像略缩图
            $config['thumb_marker'] = '_resize';
            $config['width'] = 500;
            $config['height'] = 500;
            $this->image_lib->initialize($config);
            $this->image_lib->resize();
            $this->image_lib->clear();

            //插入到相册数据库
            $this->create(array(
                'image_name' => $data['raw_name'],
                'file_ext' => $data['file_ext'],
                'image_description' => $this->input->post('image_description'),
                'album_id' => $this->input->post('album_id'),
            ));
            $data['image_description'] = $this->input->post('image_description');

            return $data;
        }
    }

    /**
     * 获取所有图片
     * 
     * @param $num	page 
     * @param $offset 偏移量
     */
    public function get_image($num, $offset) {
        $this->db->order_by("image_date", "desc");
        $query = $this->db->get($this->_table_name, $num, $offset);
        return $query->result();
    }

    /**
     * 根据图片id获取图片
     * 
     * @param $ID 图片id
     */
    public function get_image_by_id($ID) {
        $query = array();
        if (intval($ID) > 0)
            $query = $this->db->get_where($this->_table_name, array('ID' => $ID))->row();
        return $query;
    }

    /**
     * 获取下一个图片
     * @param int $ID 图片ID
     * @return array 
     */
    public function get_next_image($ID, $album_id) {
        $query = array();
        if (intval($ID) > 0) {
            $this->db->limit(1);
            $this->db->order_by('ID desc');
            $query = $this->db->get_where($this->_table_name, array('ID < ' => intval($ID), 'album_id' => $album_id))->row();
        }
        return $query;
    }

    /**
     * 获取上一个图片
     * @param int $ID 图片ID
     * @return array 
     */
    public function get_pre_image($ID, $album_id) {
        $query = array();
        if (intval($ID) > 0) {
            $this->db->limit(1);
            $this->db->order_by('ID asc');
            $query = $this->db->get_where($this->_table_name, array('ID > ' => intval($ID), 'album_id' => $album_id))->row();
        }
        return $query;
    }

    /**
     * 根据相册id获取图片
     * 
     * @param $album_id 相册id
     * @param $num	page 
     * @param $offset 偏移量
     */
    public function get_image_by_album_id($album_id, $num, $offset) {
        $query = array();
        if (intval($album_id) > 0) {
            $this->db->order_by('ID desc');
            $query = $this->db->get_where($this->_table_name, array('album_id' => $album_id), $num, $offset);
        }
        //echo $this->db->last_query();
        return $query->result();
    }

    public function get_count_by_album_id($album_id) {
        $this->db->where('album_id', $album_id);
        $this->db->from($this->_table_name);
        return $this->db->count_all_results();
    }

}
